from concurrent.futures import ThreadPoolExecutor

import requests
from DrissionPage._base.chromium import Chromium
from DrissionPage._configs.chromium_options import ChromiumOptions


# co = ChromiumOptions()
co = ChromiumOptions().headless()
# co.set_browser_path(r'/opt/google/chrome/google-chrome')
# co.set_argument('--no-sandbox')
# 设置启动时最大化
co.set_argument('--start-maximized')
# 无痕
# co.set_argument('--incognito')
# 连接浏览器
browser = Chromium(co)

tab = browser.latest_tab

tab.get('https://jncc.jinan.gov.cn/jnfdcinfo/jnfdcweb/onsaling/index_1.shtml?zn=all&pu=all&pn=&en=')

links = tab.ele('.form-list').s_eles('t:a')
# for lnk in links:
#     if lnk.link.startswith('https'):
#         print(lnk.link)
#         browser.new_tab(lnk.link)
def get_house_info(id):
    data = requests.get(f'https://jncc.jinan.gov.cn/jnfdcinfo/jnfdcweb/onsaling/viewDiv.shtml?fid={id}').json()
    print(data)

def process_community_link(lnk):
    # print(lnk.link)
    building = browser.new_tab(lnk.link)
    td_list = building.ele('#floorTable').s_eles('@tag()=td')
    with ThreadPoolExecutor(max_workers=20) as executor:
        for td in td_list:
            id = td.attr('id')
            if id:
                executor.submit(get_house_info, id)
    building.close()
def process_link(lnk):
    if lnk.link.startswith('https'):
        # print(lnk.link)
        community_tab = browser.new_tab(lnk.link)
        links = community_tab.s_eles('#houseshow')
        print(len(links))
        with ThreadPoolExecutor(max_workers=5) as executor:
            for lnk in links:
                if lnk.link.startswith('https'):
                    executor.submit(process_community_link, lnk)
        community_tab.close()

with ThreadPoolExecutor(max_workers=5) as executor:
    for lnk in links:
        executor.submit(process_link, lnk)